package EmployeeSalaryManagementSystem;

import EmployeeSalaryManagementSystem.Employee;
import EmployeeSalaryManagementSystem.RegularEmployee;
import EmployeeSalaryManagementSystem.Manager;
import EmployeeSalaryManagementSystem.InvalidWorkHoursException;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 员工薪资管理系统主程序
 */
public class Main {

    private static List<Employee> employees = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    // 数据存储方法
    public static void addEmployee(Employee employee) {
        employees.add(employee);
        System.out.println("员工 " + employee.getName() + " 添加成功!");
    }

    public static Employee findEmployeeById(String id) {
        for (Employee employee : employees) {
            if (employee.getId().equals(id)) {
                return employee;
            }
        }
        return null;
    }

    public static List<Employee> findEmployeesByName(String name) {
        List<Employee> result = new ArrayList<>();
        for (Employee employee : employees) {
            if (employee.getName().contains(name)) {
                result.add(employee);
            }
        }
        return result;
    }

    public static void displayAllEmployees() {
        System.out.println("\n=== 所有员工信息 ===");
        if (employees.isEmpty()) {
            System.out.println("没有员工信息");
        } else {
            for (int i = 0; i < employees.size(); i++) {
                System.out.println((i + 1) + ". " + employees.get(i));
            }
        }
    }

    // 薪资计算方法
    public static void calculateAllSalaries() {
        System.out.println("\n=== 批量计算薪资 ===");
        double totalSalary = 0;
        for (Employee employee : employees) {
            double salary = employee.calculateSalary();
            System.out.printf("%s (%s) 的薪资: ¥%.2f\n",
                    employee.getName(), employee.getId(), salary);
            totalSalary += salary;
        }
        System.out.printf("所有员工总薪资: ¥%.2f\n", totalSalary);
    }

    // 工资条生成方法
    public static void generateAllPaySlips() {
        System.out.println("\n=== 批量生成工资条 ===");
        for (Employee employee : employees) {
            System.out.println(employee.generatePaySlip());
            System.out.println();
        }
    }

    // 查找模式
    public static void searchMode() {
        System.out.println("\n=== 员工查找模式 ===");
        System.out.println("1. 按工号查找");
        System.out.println("2. 按姓名查找");
        System.out.println("3. 返回主菜单");
        System.out.print("请选择查找方式: ");

        int choice = scanner.nextInt();
        scanner.nextLine();

        switch (choice) {
            case 1:
                searchById();
                break;
            case 2:
                searchByName();
                break;
            case 3:
                return;
            default:
                System.out.println("无效选择!");
        }
    }

    private static void searchById() {
        System.out.print("请输入工号: ");
        String id = scanner.nextLine();
        Employee employee = findEmployeeById(id);

        if (employee != null) {
            System.out.println("\n✓ 找到员工:");
            System.out.println(employee);
            System.out.println("\n" + employee.generatePaySlip());
        } else {
            System.out.println("❌ 未找到工号为 " + id + " 的员工");
        }

        continueSearch();
    }

    private static void searchByName() {
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        List<Employee> foundEmployees = findEmployeesByName(name);

        if (!foundEmployees.isEmpty()) {
            System.out.println("\n✓ 找到 " + foundEmployees.size() + " 位员工:");
            for (int i = 0; i < foundEmployees.size(); i++) {
                System.out.println((i + 1) + ". " + foundEmployees.get(i));
            }

            System.out.print("\n请输入序号查看详细工资条 (0返回): ");
            int selection = scanner.nextInt();
            scanner.nextLine();

            if (selection > 0 && selection <= foundEmployees.size()) {
                Employee selectedEmployee = foundEmployees.get(selection - 1);
                System.out.println("\n" + selectedEmployee.generatePaySlip());
            }
        } else {
            System.out.println("❌ 未找到姓名为 " + name + " 的员工");
        }

        continueSearch();
    }

    private static void continueSearch() {
        System.out.print("\n是否继续查找? (y/n): ");
        String continueSearch = scanner.nextLine();
        if (continueSearch.equalsIgnoreCase("y")) {
            searchMode();
        }
    }

    // 系统统计
    public static void showStatistics() {
        System.out.println("\n=== 系统统计 ===");
        System.out.println("员工总数: " + employees.size());

        int regularCount = 0;
        int managerCount = 0;
        double totalSalary = 0;

        for (Employee employee : employees) {
            if (employee instanceof RegularEmployee) {
                regularCount++;
            } else if (employee instanceof Manager) {
                managerCount++;
            }
            totalSalary += employee.calculateSalary();
        }

        System.out.println("普通员工: " + regularCount + " 人");
        System.out.println("管理人员: " + managerCount + " 人");
        System.out.printf("月薪总额: ¥%.2f\n", totalSalary);
        System.out.printf("平均薪资: ¥%.2f\n", totalSalary / employees.size());
    }

    // 初始化测试数据
    public static void initializeTestData() {
        RegularEmployee emp1 = new RegularEmployee("张三", "E001", 25.0);
        emp1.setHoursWorked(45);

        RegularEmployee emp2 = new RegularEmployee("李四", "E002", 30.0);
        emp2.setHoursWorked(35);

        RegularEmployee emp3 = new RegularEmployee("王芳", "E003", 28.0);
        emp3.setHoursWorked(50);

        Manager manager1 = new Manager("王经理", "M001", 8000.0);
        manager1.setBonus(2000.0);

        Manager manager2 = new Manager("赵总监", "M002", 12000.0);
        manager2.setBonus(5000.0);

        addEmployee(emp1);
        addEmployee(emp2);
        addEmployee(emp3);
        addEmployee(manager1);
        addEmployee(manager2);
    }

    // 主菜单
    public static void showMainMenu() {
        System.out.println("\n=== 员工薪资管理系统 ===");
        System.out.println("1. 查看所有员工");
        System.out.println("2. 计算所有薪资");
        System.out.println("3. 生成所有工资条");
        System.out.println("4. 查找员工");
        System.out.println("5. 系统统计");
        System.out.println("6. 退出系统");
        System.out.print("请选择操作: ");
    }

    // 主方法
    public static void main(String[] args) {
        initializeTestData();

        System.out.println("🚀 员工薪资管理系统启动成功!");
        System.out.println("已加载 " + employees.size() + " 位员工数据");

        boolean running = true;
        while (running) {
            showMainMenu();
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    displayAllEmployees();
                    break;
                case 2:
                    calculateAllSalaries();
                    break;
                case 3:
                    generateAllPaySlips();
                    break;
                case 4:
                    searchMode();
                    break;
                case 5:
                    showStatistics();
                    break;
                case 6:
                    running = false;
                    System.out.println("感谢使用员工薪资管理系统，再见！");
                    break;
                default:
                    System.out.println("无效选择，请重新输入!");
            }
        }

        scanner.close();
    }
}